//
//  selectLayer.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/2/7.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "selectLayer.h"

@implementation selectLayer

-(void)creatView{
     self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UIView *view=[UIView new];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH*0.4);
        make.left.mas_equalTo(0);
    }];
    view.backgroundColor=[UIColor whiteColor];
    
    _tableView=[BaseTableView new];
    [view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_offset(0);
        make.width.mas_equalTo(_mainW*0.4);
    }];
    _tableView.tag=1;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
    
    _tableView2=[BaseTableView new];
    [view addSubview:_tableView2];
    [_tableView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_offset(0);
        make.width.mas_equalTo(_mainW*0.6);
    }];
    _tableView2.tag=2;
    _tableView2.delegate=self;
    _tableView2.dataSource=self;
    
    if (_abc==1) {
        NSDictionary *dic1=_shengArr[0];
        NSNumber *gtype_one_id=[dic1 objectForKey:@"id"];
        NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
        NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"id", nil];
        [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic2 andTableView:_tableView2 andViewController:self.viewController andSuccessBlock:^(id responseObject) {
            _shiArr=responseObject;
            [_tableView2 reloadData];
        } anderrorBlock:nil];
    }
    else{
        NSDictionary *dic1=_shengArr[0];
        NSNumber *gtype_one_id=[dic1 objectForKey:@"ct_id"];
        NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"ct_id", nil];
        [ZQTools AFNPostDataUrl:@"api/index/case_type" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
            _shiArr=responseObject;
            [_tableView2 reloadData];
            
        } anderrorBlock:nil];
    }
   
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==1) {
        return _shengArr.count;
    }
    return _shiArr.count;
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] init];
     if (_abc==1) {
         if (tableView.tag==1) {
             NSDictionary *dic=_shengArr[indexPath.row];
             cell.backgroundColor=[UIColor whiteColor];
             cell.textLabel.text=[dic objectForKey:@"name"];
         }else {
             NSDictionary *dic=_shiArr[indexPath.row];
             cell.backgroundColor=_LightGrey;
             cell.textLabel.text=[dic objectForKey:@"name"];
         }
     }
     else{
         if (tableView.tag==1) {
             NSDictionary *dic=_shengArr[indexPath.row];
             cell.backgroundColor=[UIColor whiteColor];
             cell.textLabel.text=[dic objectForKey:@"ct_name"];
         }else {
             NSDictionary *dic=_shiArr[indexPath.row];
             cell.backgroundColor=_LightGrey;
             cell.textLabel.text=[dic objectForKey:@"ct_name"];
         }
     }
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font=[UIFont systemFontOfSize:18];
    cell.textLabel.textColor=ColorWithRGB(49, 49, 49);
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_abc==1) {
        if (tableView.tag==1) {
            NSDictionary *dic1=_shengArr[indexPath.row];
            NSNumber *gtype_one_id=[dic1 objectForKey:@"id"];
            NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
            NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"id", nil];
            
            [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic2 andTableView:_tableView2 andViewController:self.viewController andSuccessBlock:^(id responseObject) {
                _shiArr=responseObject;
                [_tableView2 reloadData];
            } anderrorBlock:nil];
        }
        else{
            [self removeFromSuperview];
            NSDictionary *dic1=_shiArr[indexPath.row];
            _customSelectedBlock(dic1);
        }
    }
    else{
        if (tableView.tag==1) {
            NSDictionary *dic1=_shengArr[indexPath.row];
            NSNumber *gtype_one_id=[dic1 objectForKey:@"ct_id"];
            NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
            NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"ct_id", nil];
            
            [ZQTools AFNPostDataUrl:@"api/index/case_type" Dict:dic2 andTableView:_tableView2 andViewController:self.viewController andSuccessBlock:^(id responseObject) {
                _shiArr=responseObject;
                [_tableView2 reloadData];
            } anderrorBlock:nil];
        }
        else{
            [self removeFromSuperview];
            NSDictionary *dic1=_shiArr[indexPath.row];
            _customSelectedBlock(dic1);
        }
    }
  
}

- (void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsZero;
    }
}
#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self ];
    if (touchPoint.y>64+_mainH*0.4) {
        [self removeFromSuperview];
    }
    
    
}

@end
